/*
 * @Author: your name
 * @Date: 2021-04-03 19:57:29
 * @LastEditTime: 2021-04-22 23:19:21
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \chronic-back-end\module\userMod\controller.go
 */
package userMod

import (
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

func get(ctx *gin.Context) {
	user := ctx.MustGet("user").(User)
	ctx.JSON(http.StatusOK, gin.H{"user": user})
}

func allDoctors(ctx *gin.Context) {
	doctors, err := GetAllDoctors()
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
	}

	ctx.JSON(http.StatusOK, doctors)
}
func allPatients(ctx *gin.Context) {
	patients, err := GetAllPatients()
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
	}

	ctx.JSON(http.StatusOK, patients)
}
func allAdmins(ctx *gin.Context) {
	admins, err := GetAllAdmins()
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
	}

	ctx.JSON(http.StatusOK, admins)
}

func allUsers(ctx *gin.Context) {
	users, err := GetAllUsers()
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
	} else {
		ctx.JSON(http.StatusOK, users)
	}

	// ctx.JSON(http.StatusOK, "hello?")
}

type infoDto struct {
	Entry string `form:"entry" json:"entry" binding:"required"`
	Info  string `form:"info" json:"info" binding:"required"`
}

func putInformation(ctx *gin.Context) {
	user := ctx.MustGet("user").(User)
	var input infoDto
	if err := ctx.ShouldBind(&input); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	u, err := updataOneInformation(user.ID, input)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {
		ctx.JSON(http.StatusOK, u)
	}
}

type adminPutUserInfoDto struct {
	ID       string `form:"id" json:"id" binding:"required"`
	Name     string `form:"name" json:"name" binding:"required"`
	Gender   string `form:"gender" json:"gender" binding:"required"`
	Phone    string `form:"phone" json:"phone" binding:"required"`
	Birthday string `form:"birth" json:"birth" binding:"required"`
	Disease  string `form:"disease" json:"disease"`
	Deal     string `form:"deal" json:"deal"`
	Degree   string `form:"degree" json:"degree"`
	Hospital string `form:"hospital" json:"hospital"`
	Level    string `form:"level" json:"level"`
}

func adminPutUserInfo(ctx *gin.Context) {
	var input adminPutUserInfoDto
	if err := ctx.ShouldBind(&input); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	user, err2 := adminUpdataUserInfo(input)
	if err2 != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err2.Error()})
	} else {
		ctx.JSON(http.StatusOK, user)
	}
}

func adminDeleteUser(ctx *gin.Context) {
	userId := ctx.Query("id")
	id, _ := strconv.ParseUint(userId, 10, 64)
	err := deleteUser(uint(id))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {
		ctx.JSON(http.StatusOK, "删除成功")
	}
}

func getUserById(ctx *gin.Context) {
	userId := ctx.Query("id")
	id, _ := strconv.ParseUint(userId, 10, 64)
	user, err := FindUserById(uint(id))
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
	} else {
		ctx.JSON(http.StatusOK, user)
	}
}

func getUsersByCondition(ctx *gin.Context) {
	condition := ctx.Query("condition")
	users, err := findUsersByCondition(condition)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
	} else {
		ctx.JSON(http.StatusOK, users)
	}

}

func getUsersByDeal(ctx *gin.Context) {
	userDeal := ctx.Query("deal")
	deal, _ := strconv.ParseUint(userDeal, 10, 64)
	user, err := FindUsersByDeal(uint(deal))
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
	} else {
		ctx.JSON(http.StatusOK, user)
	}
}

func getDealDoctor(ctx *gin.Context) {
	user := ctx.MustGet("user").(User)
	dealDoctor, err := FindUserById(user.Deal)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
	} else {
		ctx.JSON(http.StatusOK, dealDoctor)
	}
}
